#include<bits/stdc++.h>
#define ll long long
#define int long long 
using namespace std;
ll read(){
    char c=getchar();int x=0,f=1;
    while(!isdigit(c)&&c!='-') c=getchar();
    if(c=='-') f=-1;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return f*x;
} 
const int N=1e5+120;
ll n,m,T,a1[N],c[N],a[N],cf[N],K,d[40][N],h[40][N],v[40][40];
int getcf(int x){
    int base=1,res=0;
    while(base<x) base*=2,res++;
    return res;
}
int pd(int x){
    int base=1,res=0;
    while(base<x) base*=2,res++;
    if(base==x) return true;
    return false;
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read();m=read();
    for(int i=1;i<=n;i++) a1[i]=read();
    for(int i=1;i<=m;i++) c[i]=read();
    cf[0]=1;
    K=getcf(n);
    //cout<<K<<endl;
    for(int i=1;i<=30;i++) cf[i]=cf[i-1]*2;
    for(int i=1;i<=K;i++){
        for(int j=1;j<=cf[K-i];j++){
            char c=getchar();while(!isdigit(c)) c=getchar();
            d[i][j]=c-'0';
        }
    }
    T=read();
    while(T--){
        int x[4];for(int i=1;i<=4;i++) x[i]=read();
        ll ans=0;
        for(int i=1;i<=n;i++) a[i]=a1[i]^(x[i%4]);
        for(int i=1;i<=m;i++){
            int k=getcf(c[i]);
            if(pd(c[i])){
                //cout<<"K"<<k<<endl;
                for(int j=1;j<=c[i];j++) h[1][j]=a[j],v[1][j]=j;
                //cout<<d[1][1]<<endl;
                for(int R=1;R<=k;R++){
                    for(int G=1;G<=cf[k-R];G++){
                        if(d[R][G]==0){
                            if(h[R][G*2-1]>=R) h[R+1][G]=h[R][G*2-1],v[R+1][G]=v[R][G*2-1];
                            else h[R+1][G]=h[R][G*2],v[R+1][G]=v[R][G*2];
                        }
                        else{
                            if(h[R][G*2]>=R) h[R+1][G]=h[R][G*2],v[R+1][G]=v[R][G*2];
                            else h[R+1][G]=h[R][G*2-1],v[R+1][G]=v[R][G*2-1];
                        }
                    }
                }
            }
            //cout<<c[i]<<" "<<v[k+1][1]<<endl;
            ans=(v[k+1][1]*i)^ans;
        }
        cout<<ans<<endl;
    }
    return 0;
}